package com.stephen.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by stephen on 2021-03-28 13:23 .
 * Description:
 */
public class GetServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 下边两行保证中文不乱码，必须同时设置
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        // 获取ServletContext中的属性值
        ServletContext servletContext = this.getServletContext();
        // ServletContext context = req.getServletContext(); // 不太清楚这个与上边使用this获取Context的方法有何区别
        // String name = (String)servletContext.getAttribute("name");
        // 获取初始化参数
        String name = servletContext.getInitParameter("name");
        resp.getWriter().print("你好 Servlet,name: " + name);
    }
}
